package com.gmzg.gmzgnews.me.view;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.gmzg.gmzgnews.MainActivity;
import com.gmzg.gmzgnews.MainApplication;
import com.gmzg.gmzgnews.R;
import com.gmzg.gmzgnews.me.model.User;

import org.greenrobot.eventbus.EventBus;
import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.view.annotation.Event;
import org.xutils.x;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetPasswordActivity extends AppCompatActivity {

    private static final String TAG = SetPasswordActivity.class.getSimpleName();
    public static final String SUCCESS = "success";
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.setting_pwd)
    EditText mSettingPwd;
    @BindView(R.id.comfirm_pwd)
    EditText mComfirmPwd;
    @BindView(R.id.enter_btn)
    Button mEnterBtn;
    private Bundle mBundle;
    private DbManager mDb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_password);
        ButterKnife.bind(this);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //获取传过来的信息
        mBundle = getIntent().getBundleExtra("bundle");
        mDb = ((MainApplication) getApplication()).getDb();
    }

    @OnClick(R.id.enter_btn)
    public void onClick() {
        String p1 = mSettingPwd.getText().toString().trim();
        String p2 = mComfirmPwd.getText().toString().trim();
        if (!TextUtils.isEmpty(p1)&&!TextUtils.isEmpty(p2)){
            if (p1.equals(p2)){
                //将数据保存到数据库中
                User user = new User();
                if (mBundle != null) {
                    user.setUsername(mBundle.getString("username"));
                    user.setImage(mBundle.getString("image"));

                    String phone = mBundle.getString("phoneNumber");
                    if (phone != null) {
                        user.setPhoneNumber(phone.substring(3,phone.length()));
                    }
                    user.setPassword(p1);
                    Log.d(TAG, "onClick: "+user.toString());
                    try {
                        mDb.saveBindingId(user);
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                    //注册之后就可以进入个人信息界面，这时我们需要把登陆信息保存到SharedPreferences中
                    SharedPreferences sp = getSharedPreferences(MeFragment.SP_USER, MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString(MeFragment.USERNAME, user.getUsername());
                    editor.putString(MeFragment.USER_IMAGE, user.getImage());
                    editor.apply();
                    //跳转到个人信息界面
                    EventBus.getDefault().post(user);
                    Intent intent = new Intent(this, MainActivity.class);
                    startActivity(intent);
                }
            }else {
                Toast.makeText(this, "两次密码不相等", Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }
}
